package westh.ilib.struts.action.getRecResource;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.*;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.mule.extras.client.MuleClient;
import org.mule.impl.MuleMessage;
import org.mule.umo.UMOMessage;

import westh.ilib.bean.croDomainSearch.Author;
import westh.ilib.bean.croDomainSearch.Resource;
import westh.ilib.util.PageInfo;

import java.net.InetSocketAddress;
import java.sql.SQLException;
import java.util.*;

import net.sf.json.*;
import net.sf.*;
import westh.config.Config;
public class GetRecommendedResourceAction extends Action {
	private final static Log log = LogFactory.getLog(GetRecommendedResourceAction.class);
	//get clouds Ip address
	String localhostUrl = Config.get("localhostUrl");
	String remoteUrl = Config.get("remoteUrl");
	
	public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response)
	{
		DynaActionForm f = (DynaActionForm) form;
		
		//String recListLength = f.getString("recListLength");
        String strPageNo = "0";
        if (request.getParameter("pageNo") != null)
        	strPageNo = (String)request.getParameter("pageNo");
        String strPageSize = "10";
        if (request.getParameter("pageSize") != null)
        	strPageSize = (String)request.getParameter("pageSize");
				
       // log.debug("recListLength: " + recListLength);
        log.debug("pageNo: " + strPageNo);
        log.debug("pageSize: " + strPageSize);
        
        JSONObject inJson0 = new JSONObject();
		inJson0.put("methodName", "getRecOnline");
		JSONObject inStr0 = new JSONObject();
		inStr0.put("userId", "1d7beb9c61b845caa8f360f186063995");
		//inStr0.put("recListLength",5);
		inJson0.put("jsonStr", inStr0);				
		
		log.debug("inStr0:" + inStr0.toString());
		
		String strRecOnline = "";
		try {
			MuleClient client = new MuleClient();
			//调用WebService
			UMOMessage umo = client.send("axis:http://"+localhostUrl+":4381/LIBJson/iDlibJson?method=getRecOnline",inStr0.toString(),null);
			strRecOnline = (String)umo.getPayload();
			log.debug("strRecOnline: " + strRecOnline);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
        
		log.debug("strRecOnline: " + strRecOnline);
		
		
		//String retMsg="";//返回结果信息
		String returnStr="fail";//跳转信息
		
		JSONObject jsonOut= JSONObject.fromObject(strRecOnline);
		String retCode=jsonOut.getString("retCode"); //返回代码
		
		if (retCode.equals("3001")) 
		{
			log.debug("succ");
			String result = jsonOut.getString("result");
			
			JSONArray resultArry = JSONArray.fromObject(result);
			log.debug("JSONArray=" + resultArry.size());
			//解析数组
			
			List<Resource> list = new ArrayList<Resource>();
			for(int i = 0; i < resultArry.size(); i++){
				Resource resource = new Resource();
				JSONObject jsonOut2 = JSONObject.fromObject(resultArry.get(i));
				log.debug(jsonOut2);
				resource.setRscId(jsonOut2.getString("rscId"));
				resource.setRscTitle(jsonOut2.getString("rscTitle"));
				Author firstAuthor = new Author();
				JSONObject jsonTemp1 = JSONObject.fromObject(jsonOut2.getString("rscAuthor"));
				firstAuthor.setName(jsonTemp1.getString("name"));
				if (jsonTemp1.getString("institution").equals("0"))
					firstAuthor.setInstitution("");
				else
					firstAuthor.setInstitution(jsonTemp1.getString("institution"));
				firstAuthor.setIeeeRefid(jsonTemp1.getString("ieeeRefid"));
				resource.setRscFirstAuthor(firstAuthor);
				list.add(resource);
				//break;
			}
			log.debug("array" + list.size());
			//request.setAttribute("list", list);
			returnStr = "succ";

			/**dispatch page related begin*/
			PageInfo pageInfo = new PageInfo();
			int iPageNo = pageInfo.getPageNoFromJSP(strPageNo);
			int iPageSize = pageInfo.getPageSizeFromJSP(strPageSize);
			
            pageInfo.setMaxCount(list.size());
            pageInfo.setPageNO(iPageNo);
            pageInfo.setPageSize(iPageSize);
            
            List outputList=null;
            
            int endCount=pageInfo.getMaxCount();
            
            if(endCount>iPageSize*iPageNo)
            {
            	endCount=iPageSize*iPageNo;
            	outputList=list.subList(iPageSize * (iPageNo - 1), endCount);
            }
            else if(endCount<iPageSize*iPageNo&&endCount>iPageSize * (iPageNo - 1))
            {
            	outputList=list.subList(iPageSize * (iPageNo - 1), endCount);
            }
            else
            {
            	iPageNo=pageInfo.getMaxCount()/iPageSize + 1;
            	pageInfo.setPageNO(iPageNo);
            	if(endCount>iPageSize*iPageNo)
            		endCount=iPageSize*iPageNo;
            	outputList=list.subList(iPageSize * (iPageNo - 1), endCount);
            }
            	
            //List outputList=list.subList(iPageSize * (iPageNo - 1), endCount);

            pageInfo.setPageList(outputList);

            request.setAttribute("pageInfo", pageInfo);
			/**dispatch page related end*/
		}
		else
		{
			//retMsg = jsonOut.getString("retMsg");
			//request.setAttribute("info", retMsg);
			returnStr = "fail";
		}		
		return mapping.findForward(returnStr);
	}
}
